<?php

namespace App\Admin\Controllers;

use App\Store;
use DB;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserStoreController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '店铺';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Store());

        $grid->model()->where('is_deleted', 0);

        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
        
            // 在这里添加字段过滤器
            $filter->equal('uid', '商户ID');
        
        });

        $grid->column('id', "标题")->display(function ($id) {
            $info = DB::table('stores_info')->where('store_id', $id)->get();
            $str = '';
            foreach ($info as $value) {
                $str .= ($value->type==1?'美团：':'饿了么：').$value->title."/";
            }
            return trim($str, '/');
        });
        $grid->column('uid', "商户id");
        $grid->column('cate_ids', "类别")->display(function($id){
            $arr = DB::table('categorys')->pluck('title', 'id');
            $ids = explode(',', $id);
            $str = '';
            foreach ($ids as $value) {
                $str .= $arr[$value].'/';
            }
            return trim($str, '/');
        });
        $grid->column('address', "地址");
        $grid->column('address_detail', "详细地址");
        $grid->column('lat', "纬度");
        $grid->column('lnt', "经度");
        $grid->column('linkname', '联系人');
        $grid->column('phone', '电话');
        $grid->column('wx_minisoft_path', '小程序地址')->editable();
        $grid->column('wx_minisoft_appid', '小程序id')->editable();
        $grid->column('start_time', '开始时间');
        $grid->column('end_time', '结束时间');
        // 全部关闭
        $grid->disableActions();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Store::findOrFail($id));

        $show->field('id', "标题")->as(function ($id) {
            $info = DB::table('stores_info')->where('store_id', $id)->get();
            $str = '';
            foreach ($info as $value) {
                $str .= ($value->type==1?'美团：':'饿了么：').$value->title."/";
            }
            return trim($str, '/');
        });;
        $show->field('uid', "商户ID");
        $show->cate_ids("类别")->using(['1' => '川香菜', '2' => '麻辣烫冒菜', '3' => '米粉面馆', '4' => '快餐便当', '5' => '下午茶']);
        $show->imgurl('店铺图片')->image(config('app.url'), 400);
        $show->field('address', "地址");
        $show->field('address_detail', "地址详细");
        $show->field('linkname', "联系人姓名");
        $show->field('phone', "联系人电话");
        $show->field('start_time', "开始营业时间");
        $show->field('end_time', "结束营业时间");
        $show->field('created_at', "创建时间");

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Store());

        $form->number('uid', "用户id")->disable();
        $form->text('cate_ids', __('Cate ids'));
        $form->text('title', __('Title'));
        $form->text('imgurl', __('Imgurl'));
        $form->text('lat', __('Lat'));
        $form->text('lnt', __('Lnt'));
        $form->text('address', __('Address'));
        $form->text('address_detail', __('Address detail'));
        $form->text('linkname', __('Linkname'));
        $form->mobile('phone', __('Phone'));
        $form->text('wx_minisoft_path', __('wx_minisoft_path'));
        $form->text('wx_minisoft_appid', __('wx_minisoft_appid'));
        $form->text('start_time', __('Start time'));
        $form->text('end_time', __('End time'));
        $form->switch('status', __('Status'));
        $form->switch('is_deleted', __('Is deleted'));

        return $form;
    }
}
